/*
 * ============================================================================
 *                   GNU Lesser General Public License
 * ============================================================================
 *
 *
 *
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 * 
 *
 *
 */
package rad.framework.seam;

import java.util.ArrayList;
import java.util.List;

import org.jboss.seam.Component;
import org.jboss.seam.contexts.Context;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.init.Initialization;

/**
 * Utility for accessing component metadata.
 * 
 * @author boudyacho
 */
public class ComponentUtil {

	public static List<Component> getComponentsByBeanClass(Class<?> beanClass) {
		List<Component> list = new ArrayList<Component>();

		Context context = Contexts.getApplicationContext();
		String[] names = context.getNames();

		for (String name : names) {
			if (name.endsWith(Initialization.COMPONENT_SUFFIX)) {
				Component component = (Component) context.get(name);
				if (component.getBeanClass().equals(beanClass)) {
					list.add(component);
				}
			}
		}
		return list;
	}

	public static List<Component> getComponentsByAnnotaion(Class<?> annotationType) {
		List<Component> list = new ArrayList<Component>();

		Context context = Contexts.getApplicationContext();
		String[] names = context.getNames();

		for (String name : names) {
			if (name.endsWith(Initialization.COMPONENT_SUFFIX)) {
				Component component = (Component) context.get(name);
				if (component.beanClassHasAnnotation(annotationType)) {
					list.add(component);
				}
			}
		}
		return list;
	}
}
